package com.dw.etl.core.schedu.controller;

import com.dw.etl.common.util.LocalDateUtil;
import com.dw.etl.core.api.EtlLogXxltobatchService;
import com.dw.etl.core.bean.model.EtlLogXxltobatch;
import com.dw.etl.core.bean.vo.LayMsg;
import com.dw.etl.core.schedu.core.model.XxlJobInfo;
import com.dw.etl.core.schedu.service.XxlJobService;
import io.swagger.annotations.Api;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * index controller
 *
 * @author xuxueli 2015-12-19 16:13:16
 */
@Controller
@RequestMapping("/jobplan")
@Api(tags = "调度任务计划")
@CrossOrigin(origins = "*", maxAge = 3600)
public class JobPlanController {
    private static Logger logger = LoggerFactory.getLogger(JobPlanController.class);

    @Resource
    private XxlJobService xxlJobService;
    @Autowired
    private EtlLogXxltobatchService etlLogXxltobatchService;


    @GetMapping("/getChart")
    @ResponseBody
    public LayMsg getChart(String startDate,String endDate ) {
        Map option = new HashMap();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月份是MM
        try {
            option = xxlJobService.findByIdPlan(simpleDateFormat.parse(startDate), simpleDateFormat.parse(endDate));
        } catch (Exception e) {
            e.printStackTrace();
        }
        LayMsg layMsg =  new LayMsg();
        layMsg.setCode(200);
        layMsg.setCount(option);
        return layMsg;
    }
    @GetMapping("/getPlanData")
    @ResponseBody
    public LayMsg getPlanData() {
        Date dateBegin = new Date();
        Date dateEnd = LocalDateUtil.dateCalculate(dateBegin,Calendar.DAY_OF_MONTH,1);
        List<JSONObject> list = new ArrayList<>();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月份是MM
        try {
            list = xxlJobService.getPlanData(dateBegin,dateEnd);
            List<Map<String,Object>> faildList = etlLogXxltobatchService.findFaildNum();
            Map<String,Object> dataMap = new HashMap<>();
            for(Map<String,Object> map :faildList) { //na_handler,count(1) value
                dataMap.put(map.get("na_handler").toString(),map.get("value"));
            }
            Object value;
            for(JSONObject info:list) {
                if((value=dataMap.get(info.getString("executorHandler")))!=null) {
                    info.put("id",Integer.parseInt(value.toString()));
                } else {
                    info.put("id",0);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        LayMsg layMsg =  new LayMsg();
        layMsg.setCode(200);
        layMsg.setData(list);
        return layMsg;
    }

}
